#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
const int N = 1e6 + 5;

int n, m;

void turn(vector<vector<int>>& a, int x, int y, int r, int z) {
    int size = 2 * r + 1;
    vector<vector<int>> temp(size, vector<int>(size));
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            temp[i][j] = a[x - r + i][y - r + j];
        }
    }
    if (z == 0) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                a[x - r + i][y - r + j] = temp[size - 1 - j][i];
            }
        }
    } else {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                a[x - r + i][y - r + j] = temp[j][size - 1 - i];
            }
        }
    }
}

void solve() {
    cin >> n >> m;
    vector<vector<int>> a(n + 1, vector<int>(n + 1));
    int num = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            a[i][j] = num;
            num++;
        }
    }
    while (m--) {
        int x, y, r, z;
        cin >> x >> y >> r >> z;
        turn(a, x, y, r, z);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();

    return 0;
}